package me.baicai.weixin.identity.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.baicai.weixin.identity.service.AdverTisingService;
import me.baicai.weixin.identity.service.AgentService;
import me.baicai.weixin.application.Result;
import me.baicai.weixin.identity.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @description:
 * @author: [ 超级大白菜 ]
 * @date: 2019-11-09 9:18
 */

@Api(tags = "用户操作：用户操作")
@RestController
public class IdentityController
{

    @Autowired
    private LoginService loginService;

    @Autowired
    private AdverTisingService adverTisingService;

    @ApiOperation("登录凭证获取")
    @ApiImplicitParams(value = {
        @ApiImplicitParam(name = "code", value = "通过 wx.login 获得的登录凭证", dataType = "String", required = true)})
    @PostMapping("/identity/login")
    public Result wxLogin(@RequestParam("code") String code)
    {
        return loginService.wxLogin(code);
    }

    @ApiOperation("授权登录 并注册在系统中")
    @ApiImplicitParams(
        {@ApiImplicitParam(value = "openid", name = "openid", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(value = "昵称", name = "nickName", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(value = "头像", name = "avatarUrl", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(value = "性别", name = "gender", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(value = "国家", name = "country", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(value = "省", name = "province", required = true, dataType = "int", paramType = "body"),
            @ApiImplicitParam(value = "市", name = "city", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(value = "语言", name = "language", required = true, dataType = "String", paramType = "body")
        })
    @PostMapping("/identity/registered")
    public Result registered(@RequestHeader("openid") String openid,
                             @RequestParam("nickName") String nickName,
                             @RequestParam("avatarUrl") String avatarUrl,
                             @RequestParam("gender") int gender,
                             @RequestParam("country") String country,
                             @RequestParam("province") String province,
                             @RequestParam("city") String city,
                             @RequestParam("language") String language)
    {
        return loginService.registered(openid, nickName, avatarUrl, gender, country, province,
            city, language);
    }

    @ApiOperation("代理-商家申请")
    @ApiImplicitParams(
        {@ApiImplicitParam(value = "openid", name = "openid", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(value = "姓名", name = "name", required = true, dataType = "String"),
            @ApiImplicitParam(value = "手机号码", name = "tel", required = true, dataType = "String"),
            @ApiImplicitParam(value = "微信号", name = "weixin", required = true, dataType = "String"),
            @ApiImplicitParam(value = "渠道", name = "channel", required = true, dataType = "String"),
            @ApiImplicitParam(value = "省", name = "province", required = true, dataType = "int"),
            @ApiImplicitParam(value = "市", name = "city", required = true, dataType = "String"),
            @ApiImplicitParam(value = "区", name = "area", required = true, dataType = "String"),
            @ApiImplicitParam(value = "邮编", name = "code", dataType = "String"),
            @ApiImplicitParam(value = "详细地址", name = "address", required = true, dataType = "String"),
            @ApiImplicitParam(value = "申请对象 agent:代理 shop: 商家", name = "type", required = true, dataType = "String")
        })
    @PostMapping("/identity/agentApply")
    public Result agentApply(@RequestHeader("openid") String openid,
                             @RequestParam("name") String name,
                             @RequestParam("tel") String tel,
                             @RequestParam(value = "weixin", required = false) String weixin,
                             @RequestParam("channel") String channel,
                             @RequestParam("province") String province,
                             @RequestParam("city") String city,
                             @RequestParam("area") String area,
                             @RequestParam("address") String address,
                             @RequestParam("type") String type,
                             @RequestParam(value = "size", required = false) Integer size,
                             @RequestParam(value = "shop", required = false) String shop)
    {
        return loginService.agentApply(openid, name, tel, weixin, channel, province, city, area,
            address, type, size, shop);
    }

    @ApiOperation("帮助")
    @PostMapping("/identity/help")
    public Result helpList()
    {
        return loginService.helpList();
    }

    @ApiOperation("用户首页")
    @PostMapping("/identity/index")
    public Result index(@RequestHeader("openid") String openid)
    {
        return loginService.index(openid);
    }

    @ApiOperation("用户订单")
    @PostMapping("/identity/orders")
    public Result orders(@RequestHeader("openid") String openid,
                         @RequestParam("status") Integer status,
                         @RequestParam("beginDate") String beginDate,
                         @RequestParam("endDate") String endDate,
                         @RequestParam("page") Integer page)
    {
        return loginService.orders(openid, status, beginDate, endDate, page);
    }

    @ApiOperation("用户附近商店")
    @PostMapping("/identity/nearbyShop")
    public Result nearbyShop(@RequestParam("latitude") String latitude,
                             @RequestParam("longitude") String longitude)
    {
        return loginService.nearbyShop(latitude, longitude);
    }

    @ApiOperation("代理 商家 收益明细")
    @PostMapping("/identity/income")
    public Result income(@RequestHeader("openid") String openid,
                         @RequestParam("type") String type)
    {
        return loginService.income(openid, type);
    }

    @ApiOperation("代理 商家 申请查看")
    @PostMapping("/identity/showApply")
    public Result showApply(@RequestHeader("openid") String openid,
                            @RequestParam("type") String type)
    {
        return loginService.showApply(openid, type);
    }

    @ApiOperation("申请处理")
    @PostMapping("/identity/processApply")
    public Result processApply(@RequestHeader("openid") String openid,
                               @RequestParam("id") Integer id,
                               @RequestParam(value = "commission", required = false) Integer commission,
                               @RequestParam(value = "name", required = false) String name,
                               @RequestParam("type") String type)
    {
        return loginService.processApply(openid, id, commission, name, type);
    }

    @ApiOperation("广告列表")
    @PostMapping("/identity/advertising")
    public Result advertising(
        @RequestParam(value = "city", defaultValue = "none", required = false) String city,
        @RequestParam(value = "region", defaultValue = "none", required = false) String region)
    {
        return adverTisingService.gcbAdvertising(city, region);
    }

}
